package com.blogWebAutoTest.common;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2024-03-24
 * Time:16:15
 */
public class AutotestUtils {
    public static ChromeDriver driver;

    //创建驱动对象
    public static ChromeDriver createDtiver(){
        //驱动对象已经创建好了/没有创建
        if (driver == null){
            driver = new ChromeDriver();
            //创建隐式等待,让其作用于整个作用域，不要出现代码渲染过慢而找不到元素
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

        }
        return driver;
    }
    public static List<String> getTime(){
        //文件能不能按照天的维度按照文件夹去分类进行保存
        //文件格式 20240330-164030毫秒
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyyMMdd-HHmmssSS");
        SimpleDateFormat sim2 = new SimpleDateFormat("yyyyMMdd");
        String filename = sim1.format(System.currentTimeMillis());

        String dirname = sim2.format(System.currentTimeMillis());

        List<String> list = new ArrayList<>();
        list.add(dirname);
        list.add(filename);
        return list;
    }
    /**
     * 获取屏幕截图:把所有的用例执行的结果保存下来
     */
    public static void getScreenShow(String str) throws IOException {
        List<String> list = getTime();
        //fileName需要动态的去生成
        //dir + file
        //   ./指的是当前路径下:也就是AutoTestBlog下
        //   ./src/test/java.com/blogWebAutoTest/dirname/filename
        //   ./src/test/java.com/blogWebAutoTest/20240330/loginPageLoadRight-20240330-164030毫秒.png
        String fileName = "./src/test/java/com/blogWebAutoTest/" + list.get(0) + "/" + str + "-" + list.get(1) + ".png";
        File srcfile = driver.getScreenshotAs(OutputType.FILE);
        //把屏幕截图生成的文件放到指定的路径下
        FileUtils.copyFile(srcfile,new File(fileName));
    }
}
